package cc.linker.steplink.crm.merchanth5.service;

import cc.linker.steplink.crm.commons.utils.NumberUtils;
import cc.linker.steplink.crm.merchanth5.dao.BillDao;
import cc.linker.steplink.crm.merchanth5.dao.EnrollActiveDao;
import cc.linker.steplink.crm.merchanth5.dao.MallDao;
import cc.linker.steplink.crm.response.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BillService {
    @Autowired
    private BillDao billDao;
    @Autowired
    private MallDao mallDao;
    @Autowired
    private EnrollActiveDao enrollActiveDao;

    /**
     * 反润相关账单明细列表
     *
     * @param customerId
     * @return
     */
    public List<Map<String, Object>> getBalanceDetail(String customerId) {
        List<Map<String, Object>> details = billDao.getBalanceDetail(customerId);
        details.forEach(x -> {
                    Long tradeSum = (Long) x.get("tradeSum");
                    Long sumMark = (Long) x.get("sumMark");
                    Long accountBalance = (Long) x.get("accountBalance");
                    if (sumMark > 0) {
                        x.put("tradeSum", "+" + NumberUtils.fenToYuan(tradeSum));
                    } else {
                        x.put("tradeSum", "-" + NumberUtils.fenToYuan(tradeSum));
                    }
                    if (accountBalance != null) {
                        x.put("accountBalance", NumberUtils.fenToYuan(accountBalance));
                    }
                }
        );
        return details;
    }

    public Map<String, Object> getTradeInfoByTradeId(String tradeId, String orgId) {
        Map<String, Object> map = new HashMap<>();
        map.put("orgId", orgId);
        Integer payMode = mallDao.getPayModeByOrgId(map);
        Map<String, Object> tradeInfo = billDao.getTradeInfoByTradeId(tradeId, payMode);
        String orderSn = (String) tradeInfo.get("orderSn");
        Double goodsAmount;
        if (tradeInfo.get("goodsAmount") == null && orderSn.startsWith("Y")) {
            // 报名
            boolean flag = false;
            Map<String, Object> record = enrollActiveDao.getRecordByOrderSn(orderSn);
            if (record == null) {
                record = enrollActiveDao.getFormRecord(orderSn);
                flag = true;
                if(record == null){
                    throw GlobalException.build("暂无数据");
                }
            }
            Integer payAmount = Integer.valueOf(record.get("payAmount").toString());
            goodsAmount = payAmount.doubleValue() / 100;
            tradeInfo.put("goodsAmount", goodsAmount);
            tradeInfo.put("orderAmount", goodsAmount);
            String title = "";
            if(flag){
                title = (String) record.get("orderName");
            }else{
                Map<String, Object> info = enrollActiveDao.getInfo(record);
                title = (String) info.get("title");
            }
            tradeInfo.put("goodsName", title);
        } else {
            goodsAmount = Double.valueOf(tradeInfo.get("goodsAmount").toString());
        }
        Double serviceFee = Double.valueOf(tradeInfo.get("serviceFee").toString());
        if (serviceFee == 0) {
            Long serviceScore = billDao.getServiceScoreByTradeId(tradeId);
            if (serviceScore != null && serviceScore != 0) {
                serviceFee = Double.valueOf(serviceScore);
                serviceFee = serviceFee / 100;
                tradeInfo.put("serviceFee", serviceFee);
            }
        }
        Double recomFee = Double.valueOf(tradeInfo.get("recomFee").toString());
        Double platformFee = Double.valueOf(tradeInfo.get("platformFee").toString());
        Double originalFee = Double.valueOf(tradeInfo.get("originalFee").toString());
        double i = 0;
        if (goodsAmount != 0) {
            i = (serviceFee + recomFee+originalFee) / goodsAmount * 100;
        }
        BigDecimal bigDecimal = new BigDecimal(i).setScale(0, BigDecimal.ROUND_HALF_UP);
        tradeInfo.put("rate", bigDecimal + "%");
        Long virtualRebate = Long.valueOf(tradeInfo.get("virtualRebate").toString());
        if (virtualRebate != 0) {
            tradeInfo.put("virtualRebate", "1");
        } else {
            tradeInfo.put("virtualRebate", "0");
        }
        List<String> fmt = Arrays.asList("goodsAmount", "orderAmount", "goodsFee", "serviceFee", "recomFee", "originalFee","amount", "platformFee");
        tradeInfo.forEach((x, y) -> {
            if (fmt.contains(x)) {
                y = new BigDecimal(y.toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
                tradeInfo.put(x, y);
            }
        });
        return tradeInfo;
    }
}
